package com.aviccxzx.smarthome.UserModule;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.aviccxzx.smarthome.R;
import com.aviccxzx.smarthome.base.GosUserModuleBaseActivity;
import com.gizwits.gizwifisdk.api.GizWifiSDK;
import com.gizwits.gizwifisdk.enumration.GizWifiErrorCode;


/**
 * Created by joey on 2016/10/11.
 */
public class ChangePwdActivity extends GosUserModuleBaseActivity implements View.OnClickListener {
    EditText old_pwd, new_pwd, new_set_pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setContentView(R.layout.change_pwd_lay_v19);
        } else {

            setContentView(R.layout.change_pwd_lay);
        }
        ((TextView) findViewById(R.id.title_tv)).setText("修改密码");
        initView();
    }

    private void initView() {
        old_pwd = (EditText) findViewById(R.id.old_pwd);
        new_pwd = (EditText) findViewById(R.id.new_pwd);
        new_set_pwd = (EditText) findViewById(R.id.new_set_pwd);
        findViewById(R.id.ll_title_left_view).setOnClickListener(this);
        findViewById(R.id.button_changepwd).setOnClickListener(this);
    }

    String newpwd = "";

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_title_left_view:
                finish();
                break;
            case R.id.button_changepwd:
                String token = spf.getString("Token", "");
                String oldpwd = old_pwd.getText().toString().trim();
                newpwd = new_pwd.getText().toString().trim();
                String newsetpwd = new_set_pwd.getText().toString().trim();
                if (TextUtils.isEmpty(oldpwd)) {
                    showToast("请输入旧密码");
                    return;
                }
                if (TextUtils.isEmpty(newpwd)) {
                    showToast("请输入新密码");
                    return;
                }
                if (TextUtils.isEmpty(newsetpwd)) {
                    showToast("请再次输入新密码");
                    return;
                }
                if (!newpwd.equals(newsetpwd)) {
                    showToast("两次密码输入不一致");
                    return;
                }
                String regex = "^(?!\\d+$|[a-zA-Z]+$)\\w{6,12}$";
                if (newpwd.matches(regex)){
                    progressDialog.show();
                    GizWifiSDK.sharedInstance().changeUserPassword(token, oldpwd, newpwd);
                }else {
                    showToast("新密码只能为6-12位的数字+字母");
                }
                break;
        }
    }

    @Override
    protected void didChangeUserPassword(GizWifiErrorCode result) {
        super.didChangeUserPassword(result);
        if (GizWifiErrorCode.GIZ_SDK_SUCCESS != result) {
            showToast("修改失败");
            progressDialog.dismiss();
        } else {
            showToast("修改成功");
            spf.edit().putString("PassWord", "").commit();
            finish();
            Intent intent = new Intent(ChangePwdActivity.this, GosUserLoginActivity.class);
            startActivity(intent);
        }
    }
}
